package com.zsr.example.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsr.example.entity.StatisticsDaily;
import com.zsr.example.mapper.StatisticsDailyMapper;
import com.zsr.example.service.*;
import com.zsr.example.util.constants.RedisKeyConstants;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author zsr
 * @since 2021-12-22
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {


    @Autowired
    private UcenterMemberService memberService;

    @Autowired
    private EduCourseService courseService;

    /*@Autowired
    private RedisTemplate<String, String> redisTemplate;*/

    @Autowired
    private RedisService redisService;

    @Autowired
    private BaiduTongJiApiService tongJiApiService;
    @Override
    public void createStatisticsByDay(String day, boolean isAuto) {
        //删除已存在的统计对象
        QueryWrapper<StatisticsDaily> dayQueryWrapper = new QueryWrapper<>();
        dayQueryWrapper.eq("date_calculated", day);
        baseMapper.delete(dayQueryWrapper);

        //获取统计信息https://juejin.cn/post/6875350593608056840
        int loginCount = redisService.hyperLogGet(RedisKeyConstants.LOGIN_NUM_TODAY_KEY);
        int uvCount = redisService.hyperLogGet(RedisKeyConstants.UV_NUM_TODAY_KEY);

        //合并每天的访问量到总计中
        //hyperlog.union(TOTAL_COUNT,COUNT);
        //int totalCount = hyperlog.size(TOTAL_COUNT).intValue();

        Integer registerNum = memberService.countRegisterByDay(day);//注册人数
        Integer loginNum = loginCount;//登录人数
        Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO 每日播放视频数
        Integer courseNum = courseService.selectCreateCount(day);//每日新增课程数


        //创建统计对象
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(registerNum);
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(day);
        daily.setUvNum(uvCount);

        baseMapper.insert(daily);

        //定时执行，则删除today中的数据
        if (isAuto){
            redisService.hyperLogDel(RedisKeyConstants.LOGIN_NUM_TODAY_KEY);
            redisService.hyperLogDel(RedisKeyConstants.UV_NUM_TODAY_KEY);
        }
    }

    /**
     * 获取统计数据
     *
     * @param begin
     * @param end
     * @param type
     * @return
     */
    @Override
    public Map<String, Object> getChartData(String begin, String end, String type) {
        QueryWrapper<StatisticsDaily> dayQueryWrapper = new QueryWrapper<>();
        dayQueryWrapper.select(type, "date_calculated");
        dayQueryWrapper.between("date_calculated", begin, end);

        List<StatisticsDaily> dayList = baseMapper.selectList(dayQueryWrapper);

        Map<String, Object> map = new HashMap<>();
        List<Integer> dataList = new ArrayList<Integer>();
        List<String> dateList = new ArrayList<String>();
        map.put("dataList", dataList);//数据列表
        map.put("dateList", dateList);//日期列表


        for (int i = 0; i < dayList.size(); i++) {
            StatisticsDaily daily = dayList.get(i);
            dateList.add(daily.getDateCalculated());
            switch (type) {
                case "register_num":
                    dataList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    dataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    dataList.add(daily.getCourseNum());
                    break;
                case "uv_num":
                    dataList.add(daily.getUvNum());
                    break;
                default:
                    break;
            }
        }

        return map;
    }

    /**
     * 获取后台首页常用统计
     * @return
     */
    @Override
    public Map<String, Object> showStatistics() {
        HashMap<String, Object> hashMap = new HashMap<>();
        int loginTodayCount = redisService.hyperLogGet(RedisKeyConstants.LOGIN_NUM_TODAY_KEY);
        int loginAllCount = redisService.hyperLogGet(RedisKeyConstants.LOGIN_NUM_ALL_KEY);
        JSONObject timeTrendRpt = tongJiApiService.getTimeTrendRpt();
        JSONObject commonTrackRpt = tongJiApiService.getCommonTrackRpt();
        hashMap.put("loginTodayCount",loginTodayCount);
        hashMap.put("loginAllCount",loginAllCount);
        hashMap.put("timeTrendRpt",timeTrendRpt);
        hashMap.put("commonTrackRpt",commonTrackRpt);
        hashMap.put("todayCourseCount",this.getTodayCourseCount());
        hashMap.put("allCourseCount",this.getAllCourseCount());
        return hashMap;
    }

    public int getAllCourseCount() {
        return baseMapper.getAllCourseCount();
    }

    @Override
    public int getTodayCourseCount() {
        return baseMapper.getTodayCourseCount();
    }
}
